<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类型" prop="type">
       <el-select v-model="queryParams.type" class="flex1" placeholder="请选择类型" style="width: 100%;">
          <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
       </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:divineCase:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:divineCase:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:divineCase:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="divineCaseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="类型" align="center" prop="type" >
          <template slot-scope="scope">
            <div v-if="scope.row.type == 1">卡牌问答</div>
            <div v-if="scope.row.type == 2">骰子问答</div>
            <div v-if="scope.row.type == 3">星盘问答</div>
            <div v-if="scope.row.type == 4">六爻</div>
          </template>
      </el-table-column>
      <el-table-column label="发布者" align="center" prop="nickName" />
     <el-table-column label="头像" align="center" prop="headPic">
       <template slot-scope="scope">
         <el-image style="width: 50%; height: 50px" :src="scope.row.headPic"
           :preview-src-list="[scope.row.headPic]">
         </el-image>
       </template>
     </el-table-column>
      <el-table-column label="发布时间" align="center" prop="time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title" />
     <el-table-column label="图片" align="center" prop="img">
       <template slot-scope="scope">
         <el-image style="width: 30px; height: 30px;margin-right: 5px;" :src="item"
           v-for="(item, index) in scope.row.img.split(',')" :key="index" @click="Screen(item)"></el-image>
       </template>
     </el-table-column>
      <el-table-column label="回答者" align="center" prop="toNickName" />
      <el-table-column label="头像" align="center" prop="toHeadPic">
        <template slot-scope="scope">
          <el-image style="width: 50%; height: 50px" :src="scope.row.toHeadPic"
            :preview-src-list="[scope.row.toHeadPic]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="回答时间" align="center" prop="toTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.toTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="语音地址" align="center" prop="voice">
        <template slot-scope="scope">
          <audio :src="scope.row.voice" controls style="width: 240px;"></audio>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:divineCase:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:divineCase:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改卡牌问答的案例对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" class="flex1" placeholder="请选择类型" style="width: 100%;">
            <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发布者" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入发布者昵称" />
        </el-form-item>
      <el-form-item label="头像" prop="headPic">
       <el-upload class="avatar-uploader" action="" :http-request="ossUploadFun" :show-file-list="false">
       	<img v-if="form.headPic" :src="form.headPic" style="width: 100%;height: 100px;" class="avatar">
       	<i v-else class="el-icon-plus avatar-uploader-icon" ></i>
       </el-upload>
      </el-form-item>
        <el-form-item label="时间" prop="time">
          <el-date-picker clearable
            v-model="form.time"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="图片" prop="img">
          <div style="display: flex;align-items: center;flex-wrap: wrap;">
            <img v-if="form.simpleSpectrum1.length > 0" :src="item" v-for="item in form.simpleSpectrum1" style="width: 100px;height: 100px;margin-right: 10px;margin-top: 10px;" class="avatar">
            <el-upload class="avatar-uploader" action="" :http-request="ossUploadFunSpectrum" style="margin-top: 10px;" :show-file-list="false">
            	<i class="el-icon-plus avatar-uploader-icon" ></i>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item label="回答者" prop="toNickName">
          <el-input v-model="form.toNickName" placeholder="请输入回答者昵称" />
        </el-form-item>
        <el-form-item label="头像" prop="toHeadPic">
         <el-upload class="avatar-uploader" action="" :http-request="ossUploadFunTo" :show-file-list="false">
         	<img v-if="form.toHeadPic" :src="form.toHeadPic" style="width: 100%;height: 100px;" class="avatar">
         	<i v-else class="el-icon-plus avatar-uploader-icon" ></i>
         </el-upload>
        </el-form-item>
        <el-form-item label="时间" prop="toTime">
          <el-date-picker clearable
            v-model="form.toTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择时间">
          </el-date-picker>
        </el-form-item>

       <!-- <el-form-item label="语音地址" prop="voice">
          <el-input v-model="form.voice" placeholder="请输入语音地址" />
        </el-form-item> -->

        <el-form-item label="语音"  prop="voice">
         <el-upload class="avatar-uploader" action="" :http-request="ossUploadFunVoice" :show-file-list="false">
         	<i class="el-icon-plus avatar-uploader-icon" ></i>
         </el-upload>
         <audio controls v-if="form.voice" :src="form.voice" style="width: 300px;height: 60px;" class="avatar" >
           <source :src="item"  type="audio/mpeg">
           <source :src="item"  type="audio/ogg">
           Your browser does not support this audio format.
         </audio>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
  </div>
</template>

<script>
import { listDivineCase, getDivineCase, delDivineCase, addDivineCase, updateDivineCase } from "@/api/system/divineCase";

export default {
  name: "DivineCase",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 卡牌问答的案例表格数据
      divineCaseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        nickName: null,
        headPic: null,
        time: null,
        title: null,
        img: null,
        toNickName: null,
        toHeadPic: null,
        toTime: null,
        voice: null,
      },
      // 表单参数
      form: {},
      dialogImageUrl: '',
      dialogVisible: false,
      typeOptions: [{ // 类型 1=卡牌问答 2=骰子问答 3=星盘问答 4=六爻
      	value: 1,
      	label: '卡牌问答'
      }, {
      	value: 2,
      	label: '骰子问答'
      }, {
      	value: 3,
      	label: '星盘问答'
      }, {
      	value: 4,
      	label: '六爻'
      }],
      // 表单校验
      rules: {
        type: [
          { required: true, message: "类型不能为空", trigger: "blur" }
        ],
        nickName: [
          { required: true, message: "昵称不能为空", trigger: "blur" }
        ],
        headPic: [
          { required: true, message: "头像不能为空", trigger: "change" }
        ],
        time: [
          { required: true, message: "时间不能为空", trigger: "change" }
        ],
        title: [
          { required: true, message: "标题不能为空", trigger: "change" }
        ],
        toNickName: [
          { required: true, message: "昵称不能为空", trigger: "blur" }
        ],
        toHeadPic: [
          { required: true, message: "头像不能为空", trigger: "change" }
        ],
        toTime: [
          { required: true, message: "时间不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    Screen(e) {
      this.dialogImageUrl = e
      this.dialogVisible = true;
    },
    /** 查询卡牌问答的案例列表 */
    getList() {
      this.loading = true;
      listDivineCase(this.queryParams).then(response => {
        this.divineCaseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        nickName: null,
        headPic: null,
        time: null,
        title: null,
        img: null,
        simpleSpectrum1:[],
        toNickName: null,
        toHeadPic: null,
        toTime: null,
        voice: null,
        createBy: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加卡牌问答的案例";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDivineCase(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改卡牌问答的案例";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if(this.form.simpleSpectrum1){
        this.form.img = this.form.simpleSpectrum1.toString()
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDivineCase(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDivineCase(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 图片上传
    ossUploadFun(file) {
    	this.ossUpload(file.file).then(res => {
    		this.form.headPic = res
    	})
    },
    ossUploadFunTo(file) {
    	this.ossUpload(file.file).then(res => {
    		this.form.toHeadPic = res
    	})
    },
    ossUploadFunVoice(file) {
    	this.ossUpload(file.file).then(res => {
    		this.form.voice = res
    	})
    },
    // 上传图片
    ossUploadFunSpectrum(file){
        if(file.file.type.indexOf('audio') != -1 || file.file.type.indexOf('video') != -1) {
         return this.$alert('请选择图片格式文件', '提示', {
           confirmButtonText: '确定',
         });
        }
        this.ossUpload(file.file).then(res => {
          this.form.simpleSpectrum1.push(res);
        })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除卡牌问答的案例编号为"' + ids + '"的数据项？').then(function() {
        return delDivineCase(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/divineCase/export', {
        ...this.queryParams
      }, `divineCase_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
